//
//  CTContactUsVC.m
//  digitalCurrency
//
//  Created by 小震GG on 2019/7/3.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "CTContactUsVC.h"

@interface CTContactUsVC ()
{
    UIView *faceBookView;
    UIView *wxView;
    UIImageView *fbQRImage;
    UIImageView *wxQRImage;
    UIView *realWxView;
    UIImageView *realWxQRImage;

}
@property (nonatomic, strong) UIScrollView *bgScrollView;

@end

@implementation CTContactUsVC

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = LocalizationKey(@"Contactus");
    self.view.backgroundColor = [UIColor whiteColor];
    
//    [self rightBarButton];
    
    self.bgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH-SafeAreaTopHeight)];
    [self.view addSubview:self.bgScrollView];
    self.bgScrollView.backgroundColor = [UIColor whiteColor];
    self.bgScrollView.contentSize = CGSizeMake(0, (kWindowH-SafeAreaTopHeight)+(kWindowH-SafeAreaTopHeight)/2.0);
    
    [self setFaceBookView];
    [self setWXView];
    [self setRealWXView];
}

- (void)rightBarButton {
    UIButton *saveBar = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBar.frame = CGRectMake(SCREEN_WIDTH-50, 30, 60, 40);
    [saveBar setTitle:LocalizationKey(@"save") forState:UIControlStateNormal];
    [saveBar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveBar.titleLabel.font = kFont(13);
    [saveBar addTarget:self action:@selector(saveBtnAct) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem    = [[UIBarButtonItem alloc] initWithCustomView:saveBar];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)saveBtnAct{
    
}

- (void)setFaceBookView{
    faceBookView = [[UIView alloc]init];
    [self.bgScrollView addSubview:faceBookView];
    [faceBookView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(351.0/375.0*SCREEN_WIDTH);
        make.height.equalTo(272.0/351.0*(351.0/375.0*SCREEN_WIDTH));
        make.centerX.equalTo(0);
        make.top.equalTo(11);
    }];
    faceBookView.backgroundColor = [UIColor colorWithHEX:@"#F2F2F2"];
    faceBookView.layer.masksToBounds = YES;
    faceBookView.layer.cornerRadius = 4;
    
    fbQRImage = [[UIImageView alloc]init];
    [faceBookView addSubview:fbQRImage];
    [fbQRImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(130.0/375.0*SCREEN_WIDTH);
        make.centerX.equalTo(0);
        make.top.equalTo(25);
    }];
    
//    NSString *avatars = [NSString stringWithFormat:@"%@download/register.html?avatar=%@&lang=cn",HOST,[YLUserInfo shareUserInfo].promotionCode];
//    UIImage * ScannerImag = [self buildQRScanner:avatars];
//    fbQRImage.image = ScannerImag;
    fbQRImage.image = ImageName(@"contact_fb");
    
    UILabel *contentLB = [[UILabel alloc]init];
    [faceBookView addSubview:contentLB];
    [contentLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(fbQRImage.mas_bottom).offset(18);
        make.centerX.equalTo(0);
    }];
    contentLB.text = LocalizationKey(@"Facebook");
    contentLB.textColor = tThreeColor;
    contentLB.font = kFont(15);
    
    UIButton *saveBtn = buttonTypeSystem;
    [faceBookView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(33);
        make.right.equalTo(-33);
        make.top.equalTo(contentLB.mas_bottom).offset(22);
        make.height.equalTo(44);
    }];
    [saveBtn setBackgroundColor:kColor(maincolor)];
    [saveBtn setTitle:LocalizationKey(@"Save qr code") forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveBtn.titleLabel.font = kFont(18);
    saveBtn.layer.masksToBounds = YES;
    saveBtn.layer.cornerRadius = 4.0;
    saveBtn.tag = 100;
    [saveBtn addTarget:self action:@selector(saveBtnAct:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setWXView{
    wxView = [[UIView alloc]init];
    [self.bgScrollView addSubview:wxView];
    [wxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(351.0/375.0*SCREEN_WIDTH);
        make.height.equalTo(272.0/351.0*(351.0/375.0*SCREEN_WIDTH));
        make.centerX.equalTo(0);
        make.top.equalTo(faceBookView.mas_bottom).offset(15);
    }];
    wxView.backgroundColor = [UIColor colorWithHEX:@"#F2F2F2"];
    wxView.layer.masksToBounds = YES;
    wxView.layer.cornerRadius = 4;
    
    wxQRImage = [[UIImageView alloc]init];
    [wxView addSubview:wxQRImage];
    [wxQRImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(130.0/375.0*SCREEN_WIDTH);
        make.centerX.equalTo(0);
        make.top.equalTo(25);
    }];
    
//    NSString *avatars = [NSString stringWithFormat:@"%@download/register.html?avatar=%@&lang=cn",HOST,[YLUserInfo shareUserInfo].promotionCode];
//    UIImage * ScannerImag = [self buildQRScanner:avatars];
//    wxQRImage.image = ScannerImag;
    wxQRImage.image = ImageName(@"contact_tt");
    
    UILabel *contentLB = [[UILabel alloc]init];
    [wxView addSubview:contentLB];
    [contentLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wxQRImage.mas_bottom).offset(18);
        make.centerX.equalTo(0);
    }];
    contentLB.text = LocalizationKey(@"Twitter");
    contentLB.textColor = tThreeColor;
    contentLB.font = kFont(15);
    
    UIButton *saveBtn = buttonTypeSystem;
    [wxView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(33);
        make.right.equalTo(-33);
        make.top.equalTo(contentLB.mas_bottom).offset(22);
        make.height.equalTo(44);
    }];
    [saveBtn setBackgroundColor:kColor(maincolor)];
    [saveBtn setTitle:LocalizationKey(@"Save qr code") forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveBtn.titleLabel.font = kFont(18);
    saveBtn.layer.masksToBounds = YES;
    saveBtn.layer.cornerRadius = 4.0;
    saveBtn.tag = 200;
    [saveBtn addTarget:self action:@selector(saveBtnAct:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setRealWXView{
    realWxView = [[UIView alloc]init];
    [self.bgScrollView addSubview:realWxView];
    [realWxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(351.0/375.0*SCREEN_WIDTH);
        make.height.equalTo(272.0/351.0*(351.0/375.0*SCREEN_WIDTH));
        make.centerX.equalTo(0);
        make.top.equalTo(wxView.mas_bottom).offset(15);
    }];
    realWxView.backgroundColor = [UIColor colorWithHEX:@"#F2F2F2"];
    realWxView.layer.masksToBounds = YES;
    realWxView.layer.cornerRadius = 4;
    
    realWxQRImage = [[UIImageView alloc]init];
    [realWxView addSubview:realWxQRImage];
    [realWxQRImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(130.0/375.0*SCREEN_WIDTH);
        make.centerX.equalTo(0);
        make.top.equalTo(25);
    }];
    
//    NSString *avatars = [NSString stringWithFormat:@"%@download/register.html?avatar=%@&lang=cn",HOST,[YLUserInfo shareUserInfo].promotionCode];
//    UIImage * ScannerImag = [self buildQRScanner:avatars];
//    wxQRImage.image = ScannerImag;
    realWxQRImage.image = ImageName(@"contact_wx");
    
    UILabel *contentLB = [[UILabel alloc]init];
    [realWxView addSubview:contentLB];
    [contentLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(realWxQRImage.mas_bottom).offset(18);
        make.centerX.equalTo(0);
    }];
    contentLB.text = LocalizationKey(@"WeChat");
    contentLB.textColor = tThreeColor;
    contentLB.font = kFont(15);
    
    UIButton *saveBtn = buttonTypeSystem;
    [realWxView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(33);
        make.right.equalTo(-33);
        make.top.equalTo(contentLB.mas_bottom).offset(22);
        make.height.equalTo(44);
    }];
    [saveBtn setBackgroundColor:kColor(maincolor)];
    [saveBtn setTitle:LocalizationKey(@"Save qr code") forState:UIControlStateNormal];
    [saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveBtn.titleLabel.font = kFont(18);
    saveBtn.layer.masksToBounds = YES;
    saveBtn.layer.cornerRadius = 4.0;
    saveBtn.tag = 300;
    [saveBtn addTarget:self action:@selector(saveBtnAct:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)saveBtnAct:(UIButton *)sender{
    if (sender.tag == 100) {
        UIImageWriteToSavedPhotosAlbum(fbQRImage.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    }
    else if (sender.tag == 200){
        UIImageWriteToSavedPhotosAlbum(wxQRImage.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
    else if (sender.tag == 300){
        UIImageWriteToSavedPhotosAlbum(realWxQRImage.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
}

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *msg = nil ;
    if(error) {
        msg = LocalizationKey(@"savePhotoFailure");
    }
    else {
        msg = LocalizationKey(@"savePhotoSuccess");
    }
    [self.view makeToast:msg duration:1.5 position:CSToastPositionCenter];
}

-(UIImage *)buildQRScanner:(NSString *)url
{
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2. 恢复滤镜的默认属性
    [filter setDefaults];
    // 3. 将字符串转换成NSData
    NSData *data = [url dataUsingEncoding:NSUTF8StringEncoding];
    // 4. 通过KVO设置滤镜inputMessage数据
    [filter setValue:data forKey:@"inputMessage"];
    // 5. 获得滤镜输出的图像
    CIImage *outputImage = [filter outputImage];
    [self excludeFuzzyImageFromCIImage:outputImage size:200];
    // imageView.image = [self excludeFuzzyImageFromCIImage:ciImage size:200];
    
    // 6. 将CIImage转换成UIImage，并放大显示
    return [self excludeFuzzyImageFromCIImage:outputImage size:200];
    // [UIImage imageWithCIImage:outputImage scale:20.0 orientation:UIImageOrientationUp];
}

- (UIImage *)excludeFuzzyImageFromCIImage: (CIImage *)image size: (CGFloat)size { CGRect extent = CGRectIntegral(image.extent); //通过比例计算，让最终的图像大小合理（正方形是我们想要的）
    CGFloat scale = MIN(size / CGRectGetWidth(extent), size / CGRectGetHeight(extent));
    size_t width  = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, colorSpace, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context     = [CIContext contextWithOptions: nil];
    CGImageRef bitmapImage = [context createCGImage: image fromRect: extent]; CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone); CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    //切记ARC模式下是不会对CoreFoundation框架的对象进行自动释放的，所以要我们手动释放
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    CGColorSpaceRelease(colorSpace);
    
    return [UIImage imageWithCGImage: scaledImage];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
